package net.georel.representation.categorisation.analysis;

import java.io.IOException;
import java.util.Arrays;

import rcaller.RCaller;
import rcaller.RCode;

public class ProportionalClassifier {

	private static double powerValue =1.0;
	
	public static int[] getProportionalClassification (double[] relevanceArray, int numClasses, double powerValue) throws IOException{
		
		setPowerValue(powerValue);
		
		int[] resultValues = new int[relevanceArray.length];		
		System.out.println("sorting array....");
	    	    
	    int classSum=getTotalSumofClasses(numClasses);	    
	    double[] classPercentagesMembers = getClassPercentages(classSum, numClasses);
	    int[] ClassMemberTotals = getClassMemberTotals(classPercentagesMembers,relevanceArray.length, numClasses);
	    resultValues = getClasses(ClassMemberTotals, numClasses, relevanceArray.length);
	    	    
	    return resultValues;
	}
	
	private static int getTotalSumofClasses(int numClasses){
		int totalSum=0;
		
		for(int i = 1; i<numClasses+1;i++){
			totalSum+=(Math.pow(i, powerValue));
		}
		return totalSum;
	}
	
	private static double[] getClassPercentages(int classSum, int numClasses){
		
		double[] classPercentages=new double[numClasses];
		
		for(int i =0;i<numClasses;i++){
			double classNumber = Math.pow(i+1, powerValue);
			classPercentages[i]=classNumber/classSum;
		}
		
		return classPercentages;
	}
	
	private static int[] getClassMemberTotals(double[] getClassPercentages, int numTotalMembers, int numClasses){
		
		int[] classMemberTotals=new int[numClasses];
		
		for(int i =0;i<numClasses;i++){
			classMemberTotals[i]=(int) (getClassPercentages[i]*numTotalMembers);
		}
		
		return classMemberTotals;
	}
	
	
private static int[] getClasses(int[] ClassMemberTotals, int numClasses, int numTotalMembers){
		
		int[] resultValues=new int[numTotalMembers];
		
		int classMembersTotal=0;
		int runningTotal = 1;
		for(int i =0;i<numClasses;i++){
			classMembersTotal = ClassMemberTotals[i];
			
			for(int j=runningTotal;j<runningTotal+classMembersTotal;j++){
				resultValues[j]=i+1;
			}
			runningTotal+=classMembersTotal;
		}
		
		return resultValues;
	}

public static void setPowerValue(double PowerValue) {
	powerValue = PowerValue;
}

public double getPowerValue() {
	return powerValue;
}
	
	
	
}
